<template>
  <div>
    <div class="search_box">
      <div class="search">
        <span
          class="iconfont icon-fanhui"
          @click="$router.go(-1)"
        ></span>
        <input
          type="text"
          placeholder="搜索歌曲、歌手、专辑"
          v-model="keywords"
        />
        <span
          class="iconfont icon-shanchu"
          @click="empty"
          v-show="keywords"
        ></span>
      </div>
      <div class="test" v-show="!keywords">
        <p class="header">热门搜索</p>
        <div class="txt">
          <p
            v-for="(item, index) in search"
            :key="item.score"
            v-show="index < 10"
            @click="getInto(index)"
          >
            {{ item.searchWord }}
          </p>
        </div>
        <div class="history">
          <div class="header">
            <p>历史搜索</p>
            <span @click="clearAll()" v-show="history.length != 0">清除</span>
          </div>
          <p
            class="history_txt"
            v-for="(item, index) in history"
            :key="item"
            @click="getHistoryInto(index)"
          >
            {{ item }}
          <span class="iconfont icon-close" @click="del(index)"></span>
          </p>
        </div>
      </div>
      <div class="songslist" v-if="keywords">
        <ul>
          <p class="ul_head">最佳匹配</p>
          <li class="singer" @click="$router.push({path: `/singer/singerlist?id=${singer.id}`})">
            <img :src="singer.picUrl" width="45px" height="45px" />
            <span>歌手：{{ singer.name }}</span>
          </li>
          <li class="singer" @click="$router.push({path: `/rank/ranklist?id=${singerlist.id}`})">
            <img :src="singerlist.coverImgUrl" width="45px" height="45px" />
            <span>歌单：{{ singerlist.name }}</span>
          </li>
          <li
            v-for="(item, index) in songs"
            :key="item.id"
            @click="play(index)"
          >
            {{ item.name }}<br /><span>{{ item.ar[0].name }}</span>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import { getSearch, getKeywords } from "../../api/search";
export default {
  data() {
    return {
      search: null,
      keywords: null,
      songs: null,
      history: [],
      singer: { name: "", picUrl: "" }, //歌手
      singerlist: { name: "", coverImgUrl: "" }, //歌手歌单
    };
  },
  created() {
    getSearch().then((data) => {
      // console.log(data);
      this.search = data.data;
    });
  },
  methods: {
    // 点击热门推荐搜索
    getInto(index) {
      // console.log(this.search[index].searchWord);
      this.keywords = this.search[index].searchWord;
      let i = this.history.findIndex(
        (itme) => itme == this.search[index].searchWord
      );
      if (i == -1) {
        this.history.push(this.search[index].searchWord);
      }
    },
    //历史重复搜索
    getHistoryInto(index) {
      console.log(this.history, index);
      this.keywords = this.history[index];
    },
    //清空搜索框
    empty() {
      this.keywords = this.songs = null;
    },
    //清空历史记录
    clearAll(){
      this.history=[];
    },
    //删除单个历史记录
    del(index){
      this.history.splice(index,1);
    },
    //点击歌曲播放
    play(index) {
      console.log(this.songs[index].id);
      let i = this.history.findIndex((itme) => itme == this.keywords);
      if (i == -1) {
        this.history.push(this.keywords);
      }
      this.$emit("get-play-id", this.songs[index].id);
    }
  },
  watch: {
    //显示歌曲
    keywords() {
      if (this.keywords) {
        //获取歌曲
        getKeywords({ keywords: this.keywords }).then((item) => {
          // console.log(item.result.songs);
          this.songs = item.result.songs;
        });
        //获取歌手
        getKeywords({ keywords: this.keywords, type: 100 }).then((item) => {
          // console.log(item);
          this.singer = item.result.artists[0];
          // console.log(this.singer);
        });
        //获取歌单
        getKeywords({ keywords: this.keywords, type: 1000 }).then((item) => {
          // console.log(item);
          this.singerlist = item.result.playlists[1];
        });
      }
    },
  },
};
</script>

<style lang="less">
.search_box {
  width: 100vw;
  height: 100vh;
  background-color: #f2f3f4;
  .search {
    min-height: 40px;
    height: 5.8%;
    background-color: #d43c33;
    padding-left: 14px;
    .icon-fanhui {
      font-size: 16px;
      line-height: 35px;
      color: #fff;
      vertical-align: middle;
    }
    .icon-shanchu {
      font-size: 20px;
      line-height: 35px;
      padding-left: 5px;
      color: #fff;
      vertical-align: middle;
    }
    input {
      height: 20px;
      line-height: 35px;
      margin-left: 15px;
      border: 0;
      border-bottom: 1px solid #e29d98;
      width: 80%;
      font-size: 14px;
      padding-bottom: 3px;
      background-color: transparent;
      outline: none;
      color: #fff;
      &::-webkit-input-placeholder {
        color: #eba8a4;
      }
    }
  }
  .test {
    width: 95%;
    margin: 0 auto;
    .header {
      background-color: #f2f3f4;
      padding-left: 12px;
      font-size: 12px;
      color: #666;
    }
    .txt {
      display: flex;
      flex-wrap: wrap;
      padding: 0 12px;
      p {
        padding: 5px 3px;
        border: 1px solid #dadadb;
        border-radius: 7px;
        font-size: 12px;
        margin-right: 7px;
        margin-bottom: 6px;
        color: #515252;
      }
    }
    .history {
      .header {
        background-color: #f2f3f4;
        padding: 0 12px;
        font-size: 12px;
        justify-content: space-between;
        color: #666;
      }
      .history_txt {
        font-size: 12px;
        padding-left: 12px;
        padding-bottom: 10px;
        span{
          float: right;
          padding-right:18px ;
        }
      }
    }
  }
  .songslist {
    ul {
      .ul_head {
        font-size: 12px;
        color: #d84f45;
        padding: 8px 0 0 18px;
      }
      .singer {
        display: flex;
        line-height: 45px;
        span {
          white-space: nowrap;
          overflow: hidden;
          width: 80%;
          text-overflow:ellipsis;
          padding-left: 10px;
          font-size: 14px;
        }
      }
      img {
        padding-bottom: 5px;
      }
      li {
        height: 7.6%;
        min-height: 40px;
        border-bottom: 1px solid #dadadb;
        padding-top: 10px;
        padding-left: 18px;
        font-size: 14px;
        color: #000;
        span {
          font-size: 12px;
          color: #515252;
        }
      }
    }
  }
}
</style>